package com.lion.tag.grid;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import com.lion.tag.LionBaseTag;
import com.lion.tag.util.AppendUtil;

public class DataGrid extends LionBaseTag {
	private static final long serialVersionUID = 1918236148028781821L;
	private String title;
	private String singleSelect = "false";// 是否单选模式(true|false 默认false)
	private String collapsible = "false";// 是否可折叠(true|false 默认false)
	private String url;// 异步加载地址
	private String method = "post";// 请求方式(get|post 默认post)
	private String onClickCell;// 单击单元格事件处理函数
	private String icon;// 图标
	private String styler;// 单元格样式处理函数
	private String rownumbers;// 行号
	private String checkbox;// 是否带复选框
	private String pagination;// 是否带分页控件(true|false)
	private String autoRowHeight;// 自动行高(true|false)
	private String pageSize;// 每页最大行数
	private String toolbar;// 工具栏控件id
	private String showFooter;// 显示底部
	private String multiSort;// 多列排序
	private String remoteSort;
	private String fitColumns;// 自适应列
	private String onClickRow;// 单击单行事件处理函数

	@Override
	public int doStartTag() throws JspException {
		StringBuilder sb = new StringBuilder(50);
		sb.append("<table class=\"easyui-datagrid\" ");
		sb = AppendUtil.addAttribute(sb, "id", id);
		sb = AppendUtil.addAttribute(sb, "title", title);
		sb = AppendUtil.addAttribute(sb, "style", style);

		int hasData = 0;
		AppendUtil.appendOptionData(sb, hasData, "singleSelect", singleSelect,false);
		AppendUtil.appendOptionData(sb, hasData, "collapsible", collapsible,false);
		AppendUtil.appendOptionData(sb, hasData, "url", url, true);
		AppendUtil.appendOptionData(sb, hasData, "method", method, true);
		AppendUtil.appendOptionData(sb, hasData, "onClickCell", onClickCell,false);
		AppendUtil.appendOptionData(sb, hasData, "iconCls", icon, true);
		AppendUtil.appendOptionData(sb, hasData, "styler", styler, false);
		AppendUtil.appendOptionData(sb, hasData, "rownumbers", rownumbers,false);
		AppendUtil.appendOptionData(sb, hasData, "pagination", pagination,false);
		AppendUtil.appendOptionData(sb, hasData, "autoRowHeight",autoRowHeight, false);
		AppendUtil.appendOptionData(sb, hasData, "pageSize", pageSize, false);
		AppendUtil.appendOptionData(sb, hasData, "showFooter", showFooter,false);
		AppendUtil.appendOptionData(sb, hasData, "multiSort", multiSort, false);
		AppendUtil.appendOptionData(sb, hasData, "remoteSort", remoteSort,false);
		AppendUtil.appendOptionData(sb, hasData, "fitColumns", fitColumns,false);
		AppendUtil.appendOptionData(sb, hasData, "onClickRow", onClickRow,false);
		if (toolbar.charAt(0) != '#') {
			toolbar = "#" + toolbar;
		}
		AppendUtil.appendOptionData(sb, hasData, "toolbar", toolbar, false);
		if(hasData==1){
			sb.append("\">");
		}else{
			sb.append(">");
		}
		if (checkbox != null && checkbox.equals("true")) {
			sb.append("<th data-options=\"field:'ck',checkbox:true\"></th>");
		}
		try {
			pageContext.getOut().write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;// 表示标签之间的内容被正常执行
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().write("</table>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the singleSelect
	 */
	public String getSingleSelect() {
		return singleSelect;
	}

	/**
	 * @param singleSelect
	 *            the singleSelect to set
	 */
	public void setSingleSelect(String singleSelect) {
		this.singleSelect = singleSelect;
	}

	/**
	 * @return the collapsible
	 */
	public String getCollapsible() {
		return collapsible;
	}

	/**
	 * @param collapsible
	 *            the collapsible to set
	 */
	public void setCollapsible(String collapsible) {
		this.collapsible = collapsible;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the method
	 */
	public String getMethod() {
		return method;
	}

	/**
	 * @param method
	 *            the method to set
	 */
	public void setMethod(String method) {
		this.method = method;
	}

	/**
	 * @return the onClickCell
	 */
	public String getOnClickCell() {
		return onClickCell;
	}

	/**
	 * @param onClickCell
	 *            the onClickCell to set
	 */
	public void setOnClickCell(String onClickCell) {
		this.onClickCell = onClickCell;
	}

	/**
	 * @return the icon
	 */
	public String getIcon() {
		return icon;
	}

	/**
	 * @param icon
	 *            the icon to set
	 */
	public void setIcon(String icon) {
		this.icon = icon;
	}

	/**
	 * @return the styler
	 */
	public String getStyler() {
		return styler;
	}

	/**
	 * @param styler
	 *            the styler to set
	 */
	public void setStyler(String styler) {
		this.styler = styler;
	}

	/**
	 * @return the rownumbers
	 */
	public String getRownumbers() {
		return rownumbers;
	}

	/**
	 * @param rownumbers
	 *            the rownumbers to set
	 */
	public void setRownumbers(String rownumbers) {
		this.rownumbers = rownumbers;
	}

	/**
	 * @return the checkbox
	 */
	public String getCheckbox() {
		return checkbox;
	}

	/**
	 * @param checkbox
	 *            the checkbox to set
	 */
	public void setCheckbox(String checkbox) {
		this.checkbox = checkbox;
	}

	/**
	 * @return the pagination
	 */
	public String getPagination() {
		return pagination;
	}

	/**
	 * @param pagination
	 *            the pagination to set
	 */
	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

	/**
	 * @return the autoRowHeight
	 */
	public String getAutoRowHeight() {
		return autoRowHeight;
	}

	/**
	 * @param autoRowHeight
	 *            the autoRowHeight to set
	 */
	public void setAutoRowHeight(String autoRowHeight) {
		this.autoRowHeight = autoRowHeight;
	}

	/**
	 * @return the pageSize
	 */
	public String getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize
	 *            the pageSize to set
	 */
	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the toolbar
	 */
	public String getToolbar() {
		return toolbar;
	}

	/**
	 * @param toolbar
	 *            the toolbar to set
	 */
	public void setToolbar(String toolbar) {
		this.toolbar = toolbar;
	}

	/**
	 * @return the showFooter
	 */
	public String getShowFooter() {
		return showFooter;
	}

	/**
	 * @param showFooter
	 *            the showFooter to set
	 */
	public void setShowFooter(String showFooter) {
		this.showFooter = showFooter;
	}

	/**
	 * @return the multiSort
	 */
	public String getMultiSort() {
		return multiSort;
	}

	/**
	 * @param multiSort
	 *            the multiSort to set
	 */
	public void setMultiSort(String multiSort) {
		this.multiSort = multiSort;
	}

	/**
	 * @return the remoteSort
	 */
	public String getRemoteSort() {
		return remoteSort;
	}

	/**
	 * @param remoteSort
	 *            the remoteSort to set
	 */
	public void setRemoteSort(String remoteSort) {
		this.remoteSort = remoteSort;
	}

	/**
	 * @return the fitColumns
	 */
	public String getFitColumns() {
		return fitColumns;
	}

	/**
	 * @param fitColumns
	 *            the fitColumns to set
	 */
	public void setFitColumns(String fitColumns) {
		this.fitColumns = fitColumns;
	}

	/**
	 * @return the onClickRow
	 */
	public String getOnClickRow() {
		return onClickRow;
	}

	/**
	 * @param onClickRow
	 *            the onClickRow to set
	 */
	public void setOnClickRow(String onClickRow) {
		this.onClickRow = onClickRow;
	}

}
